$(function(){
	var random = new Date().getTime();
	var addDialogId = "add_" + random;
	var modifyDialogId = "modify_" +random;
	var _form_name_id = 'name_' + random;
	var _form_password_id = "password_" + random;
	var _form_gender_id ="gender_" + random;
	var _form_email_id = "email_" + random;
	var _form_id = "id_" + random;
	var _form_name_m_id ="name_m_"+random;
	var _form_gender_m_id="gender_m_"+random;
	var _form_email_m_id="email_m_"+random;
	
	var dialogOpts = {
			autoOpen : false,
			width : 350,
			modal : true,
			buttons : {
				"确定" : function() {
					var param={
							username: $("#"+_form_name_id).val(),
							password: $("#"+_form_password_id).val(),
							gender: $("#"+_form_gender_id).val(),
							email:$("#"+_form_email_id).val()
						}
						$.ajax({
							url : 'user/add',
							type : 'POST',
							data : param,
							dataType : 'json',
							complete : function(XHR, TS) {
								$("#"+_form_name_id).val('');
								$("#"+_form_password_id).val('');
								$("#"+_form_gender_id).val('');
								$("#"+_form_email_id).val('');
								if (TS == "success") {
									var ar=XHR.responseJSON;
									if(ar.success){
										refresh();
									}
									else{
										alert('失败！');
									}
								}
							}
						});
					$("#"+addDialogId).dialog("close");
				},
				"取消" : function() {
					$("#"+addDialogId).dialog("close");
				}
			}
	}
	var dialogOptsModify = {
			autoOpen : false,
			width : 350,
			modal : true,
			buttons : {
				"确定" : function() {
					var param={
							id: $("#"+_form_id).val(),
							username: $("#"+_form_name_m_id).val(),
							gender: $("#"+_form_gender_m_id).val(),
							email: $("#"+_form_email_m_id).val()
						}
						$.ajax({
							url : 'user/update',
							type : 'POST',
							data : param,
							dataType : 'json',
							complete : function(XHR, TS) {
								$("#"+_form_id).val('');
								$("#"+_form_name_m_id).val('');
								$("#"+_form_gender_m_id).val('');
								$("#"+_form_email_m_id).val('');
								if (TS == "success") {
									var ar=XHR.responseJSON;
									if(ar.success){
										if(currentItem){
											currentItem.data.username = ar.data.username;
											currentItem.data.gender = ar.data.gender;
											currentItem.data.email = ar.data.email;
											currentItem.update();
										}
									}
									else{
										alert('失败！');
									}
								}
							}
						});
					$("#"+modifyDialogId).dialog("close");
				},
				"取消" : function() {
					$("#"+modifyDialogId).dialog("close");
				}
			}
	}
	var addMark = '<div id="'+addDialogId+'" title="添加用户">'+
	'<table style="width:100%;">'+
		'<tr>'+
		'<td style="width:60px;">用户名:</td>'+
		'<td><input id="'+_form_name_id+'" type="text"/></td>'+
		'</tr>'+
		
		'<tr>'+
		'<td style="width:60px;">密码:</td>'+
		'<td><input id="'+_form_password_id+'" type="text"/></td>'+
		'</tr>'+
		
		'<tr>'+
		'<td>性别:</td>'+
		'<td><input id="'+_form_gender_id+'" type="text"/></td>'+
		'</tr>'+
		
		'<tr>'+
		'<td>邮箱:</td>'+
		'<td><input id="'+_form_email_id+'" type="text"/></td>'+
		'</tr>'+
	'</table>'
	'</div>';
	$(addMark).appendTo(document.body).dialog(dialogOpts);
	
	$("#add").click(function(){
		$("#" + addDialogId).dialog("open");
	});
	
	var modifyMark = '<div id="'+modifyDialogId+'" title="修改用户">'+
	'<table style="width:100%;">'+
		'<tr>'+
		'<td style="width:60px;">姓名:<input type="hidden" id="'+_form_id+'"/></td>'+
		'<td><input id="'+_form_name_m_id+'" type="text"/></td>'+
		'</tr>'+
		
		'<tr>'+
		'<td>性别:</td>'+
		'<td><input id="'+_form_gender_m_id+'" type="text"/></td>'+
		'</tr>'+
		
		'<tr>'+
		'<td>邮箱:</td>'+
		'<td><input id="'+_form_email_m_id+'" type="text"/></td>'+
		'</tr>'+
	'</table>'
	'</div>';
	$(modifyMark).appendTo(document.body).dialog(dialogOptsModify);
	var currentItem;
	var mark = "<tr class=\"jks-table-body\">" +
			"<td><input type=\"checkbox\"/></td>"+
			"<td>{{= index}}</td>" +
			"<td>{{= username}}</td>" +
			"<td>{{= gender}}</td>" +
			"<td>{{= email}}</td>" +
			"<td><a>编辑</a></td>"+
			"</tr>";
	
	function refresh(){
		$("#user tr[class*=jks-table-body]").remove();
		$.ajax({
			url: "user/all",
			dataType: "json",
			complete: function(XHR, TS){
				if(TS == "success"){
					var ar = XHR.responseJSON;
					if(ar.success){
						if(ar.data){
							for(var i=0; i<ar.data.length; i++){
								ar.data[i].index=i+1;
							}
							$.tmpl(mark, ar.data).appendTo("#user");
						}
					}
				}
			}
		});
	}
	refresh();
	$("#refresh").click(function(){
		refresh();
	});
	$("#user").delegate("tr > td:last-child", "click", function(){
		var item = $.tmplItem(this);
		$("#"+_form_id).val(item.data.id);
		$("#"+_form_name_m_id).val(item.data.username);
		$("#"+_form_gender_m_id).val(item.data.gender);
		$("#"+_form_email_m_id).val(item.data.email);
		
		$("#"+modifyDialogId).dialog("open");
		currentItem = item;
	});
});